package com.homegoods.classify.persenter;

import android.os.Bundle;
import android.support.v4.app.Fragment;

import com.alibaba.android.arouter.launcher.ARouter;
import com.homegoods.aroutercomment.ARouterPath;
import com.homegoods.classify.ClassifyApiUtils;
import com.homegoods.classify.model.ClassifyModel;
import com.homegoods.classify.view.ClassifyView;
import com.kedll.library.mvp.BasePresenter;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * add shibo.zheng
 * 19/06/20
 * 分类fragment的presenter
 */
public class ZPClassifyPresenter extends BasePresenter<ClassifyView> {
    private List<Fragment> fragments;
    private List<String> titles;

    public ZPClassifyPresenter(ClassifyView view) {
        super(view);
    }

    public void getClassifyModel() {
        ClassifyApiUtils.getZPClassifyList(classifyModel -> loadData(classifyModel), throwable -> {});
    }

    private void loadData(ClassifyModel models) {
        fragments = new ArrayList<>();
        titles = new LinkedList<>();
        for (int i = 0, j = 0; i < (models.getProductGroupList().size() + 1); i++) {
            Bundle bundle = new Bundle();
            Fragment fragment;
            if (i == 0) {
                titles.add("品牌");
                fragment = (Fragment) ARouter.getInstance().build(ARouterPath.PATH_ZP_CLASSIFY_BRAND).navigation();
                bundle.putSerializable("models", models);
            } else {
                ClassifyModel.ProductGroupListBean groupListBean = models.getProductGroupList().get(j++);
                titles.add(groupListBean.getGroupName());
                fragment = (Fragment) ARouter.getInstance().build(ARouterPath.PATH_ZP_CLASSIFY_OTHER).navigation();
                bundle.putSerializable("classifyModel", groupListBean);
            }
            fragment.setArguments(bundle);  //将classifyModel传入到对应的fragemnt中去
            fragments.add(fragment);
        }
        mView.initZPViewPager(fragments, titles);
    }
}
